package com.elite;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.sql.Connection;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPFile;



public class ZSMS implements Runnable{
	private static final Log log=LogFactory.getLog(ZSMS.class);
	public static com.elite.FtpBean ftpbean;
	public static String times;
	private boolean flag = true; 
	private static int Hz =1; 
	private static String sql =" select * from Documentout where DOCTYPE=3 and ((Status=0) or (Status!=1 and TrialCount<TrialCountLimit)) "; 
	private static String update_sql="";
	public static String username;
	public static String username1;
	public static String password;
	public static String password1;
	public static String XHH;
	public static String XHH1;
	private static String LastTryDateTime="";
	private static String FinishDateTime="";
	private static SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
	
	

    public static com.elite.FtpBean getFtpbean() {
		return ftpbean;
	}
	public static void setFtpbean(com.elite.FtpBean ftpbean) {
		ZSMS.ftpbean = ftpbean;
	}
	public static String getTimes() {
		return times;
	}
	public static void setTimes(String times) {
		ZSMS.times = times;
	}
	public static String getUsername() {
		return username;
	}
	public static void setUsername(String username) {
		ZSMS.username = username;
	}
	public static String getUsername1() {
		return username1;
	}
	public static void setUsername1(String username1) {
		ZSMS.username1 = username1;
	}
	public static String getPassword() {
		return password;
	}
	public static void setPassword(String password) {
		ZSMS.password = password;
	}
	public static String getPassword1() {
		return password1;
	}
	public static void setPassword1(String password1) {
		ZSMS.password1 = password1;
	}
	public static String getXHH() {
		return XHH;
	}
	public static void setXHH(String xhh) {
		XHH = xhh;
	}
	public static String getXHH1() {
		return XHH1;
	}
	public static void setXHH1(String xhh1) {
		XHH1 = xhh1;
	}
	public static String getdesc(String a){
    	if(a.equals("-1")){
    		return "发送失败--检查是否有余额";
    	}else if(a.equals("-2")){
    		return "参数错误--看参数传的是否均正常,请调试程序查看各参数";
    	}else if(a.equals("-3")){
    		return "序列号密码不正确--1.序列号未注册2.密码加密不正确3.密码已被修改";
    	}else if(a.equals("0")){
    		return "没有需要取得的数据--取用户回复就出现0的返回值,表示没有回复数据";
    	}else{
    		return "未知错误";
    	}
    }
	public void run(){
		log.info("---------------------中视短信接口----------------------------");
		while(flag){
			try{
				log.info("sss:"+times);
				Hz=Integer.parseInt(times);
			}catch(Exception e){
				log.info("错误:"+e);
				log.info("因为获取运行频率出错，改为默认频率1分钟运行");
			}
			log.info("扫描documentout表中未处理的记录："+sql);
			List list=null;
			try{
				list=ftpbean.chaxun(sql);
			}catch(Exception e){
				log.info("获取扫描结果错误："+e);
			}
			if(list!=null){
				for(int i=0;i<list.size();i++){
					Map map=(Map)list.get(i); 
					String StringFrield2=Util.filterForObject(map.get("StringFrield2"));
					String mobile=Util.filterForObject(map.get("DocumentTo"));
					String DocumentPath=Util.filterForObject(map.get("DocumentPath"));
					String TrialCountLimit=Util.filterForObject(map.get("TrialCountLimit"));
					String DocumentOut_GUID=Util.filterForObject(map.get("DocumentOut_GUID"));
					
					if(1==1){
						if(1==1){
							String url1="";
							URL url=null;
							URLConnection conn=null;
							OutputStreamWriter wr=null;
							BufferedReader rd=null;
							String line="";
							try{
								DocumentPath=URLEncoder.encode(DocumentPath,"GB2312");
							}catch(Exception e){
								 log.info("DocumentPath转码错误："+e);
							}
							log.info("DocumentPath转码："+DocumentPath);
							
							  Date d = new Date();
							  long longtime = d.getTime();
							  String longtime1=longtime+"";
							  longtime1=XHH+longtime1.substring(longtime1.length()-5, longtime1.length());
							  String longtime2=XHH1+longtime1.substring(longtime1.length()-5, longtime1.length());
							 // System.out.println(longtime1);
							  
							url1="http://sdk2.entinfo.cn/z_send.aspx?sn="+username1+
							"&pwd="+password1+"&mobile="+mobile+"&content="+DocumentPath+"&ext=";
														
//							if(StringFrield2.equals("1")){
//								url1="http://220.194.54.161:9010/index.aspx?action=sendsms&username="+username1+
//										"&password="+password1+"&mobile="+mobile+"&content="+DocumentPath+"&dstime=&xh=120";
//							}else if(StringFrield2.equals("2")){
//								url1="http://220.194.54.161:9010/index.aspx?action=sendsms&username="+username+
//								"&password="+password+"&mobile="+mobile+"&content="+DocumentPath+"&dstime=&xh=120";
//							}
//							
							Date date=new Date();
							LastTryDateTime = formatter.format(date);
							try{
							
								 log.info("发送url："+url1);
								 url=new URL(url1); 
							}catch(Exception e){
								log.info("new URL错误："+e);
							}
							try{
								 conn=url.openConnection(); 
							}catch(Exception e){ 
								log.info("url.openConnection()错误："+e);
							}
							conn.setDoOutput(true); 
							Date date1=new Date();
							FinishDateTime = formatter.format(date1);
//							try{
//								 wr=new OutputStreamWriter(conn.getOutputStream()); 
//							}catch(Exception e){
//								log.info("new OutputStreamWriter错误："+e);
//							}
							try{
						        rd=new BufferedReader(new InputStreamReader(conn.getInputStream())); 
							}catch(Exception e){
								log.info("new BufferedReader错误："+e);
							}
							try{
								line =rd.readLine(); 
							}catch(Exception e){
								log.info("readLine()错误："+e);
							}
							if(line!=null&&line!=""){
								log.info("返回结果:"+line);
								if(line.equals("1")){
									update_sql=" update DocumentOut set stringfield2='"+longtime2+"',Status=1,TrialCount=TrialCount+1,LastTryDateTime='"+LastTryDateTime+"',FinishDateTime='"+FinishDateTime+"',ModifiedDate=getdate() where DocumentOut_GUID='"+DocumentOut_GUID+"'";
									
								}else{
									update_sql=" update DocumentOut set FailCode="+line+",FailDescription='"+getdesc(line)+"',stringfield2='"+longtime2+"',Status=5,TrialCount=TrialCount+1,LastTryDateTime='"+LastTryDateTime+"',FinishDateTime='"+FinishDateTime+"',ModifiedDate=getdate() where DocumentOut_GUID='"+DocumentOut_GUID+"'";
								}
								log.info("update_sql:"+update_sql);
								try{
									int a=ftpbean.update(update_sql);
									log.info("是否更新DocumentOut成功:"+a);
								}catch(Exception e){
									log.info("更新DocumentOut出错:"+e);
								}
								
							}else{
								log.info("DocumentOut查询无结果");
							}

							
						}else{
							log.info("手机号"+mobile+"StringFrield2字段错误");
						}
						
					}else{
						log.info("手机号"+mobile+"StringFrield2字段错误");
					}
				}
				
			}else{
				log.info("没有需要发送的短信");
			}
                
			try { 
				Thread.sleep(Hz*1000); 
				} 
				catch (Exception e) {
					log.info("定时错误:"+e);
					} 
		}
	}

	

}





















